const { getApi, postApi, putApi } = require("./util");
export function login() {
  return new Promise((resolve, reject) => {
    wx.showLoading({
      title: "登陆中",
    });
    wx.login({
      success: (res) => {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: "http://rinke.wicp.net:8089/auth/login",
            method: "POST",
            header: {
              Authorization: res.code,
            },
            success: (loginRes) => {
              wx.setStorage({
                key: "token",
                data: loginRes.data.data.token,
              });
              resolve();
            },
            fail: (loginRes) => {
              reject(loginRes);
            },
            complete: (res) => {
              wx.hideLoading();
            },
          });
        } else {
          reject(res);
        }
      },
    });
  });
}

export function updateMe(userData) {
  return putApi("user/me", userData);
}

export function getMe() {
  return getApi("user/me");
}

export function createObject(objData) {
  return postApi("object/create", objData);
}

export function getObject(id) {
  return getApi(`object/${id}`);
}

export function getObjects(page, size) {
  return getApi(`object/last?page=${page}&size=${size}`);
}

export function getObjectsByTypeID(id) {
  return getApi(`object/type/${id}`);
}

export function createComment(commentData) {
  return postApi("comment/create", commentData);
}

export function getCommentByID(id) {
  return getApi("comment/" + id);
}

export function getMyComments() {
  return getApi("comment/my");
}

export function getCommentsByCommentID(id) {
  return getApi(`comment/comment/${id}`);
}

export function getCommentsByObjectID(id) {
  return getApi("comment/object/" + id);
}

export function getComments(page, size) {
  return getApi(`comment/last?page=${page}&size=${size}`);
}

export function updateComment(commentData) {
  return putApi("comment/update", commentData);
}

export function getCommentLikeStatus(id) {
  return getApi(`comment/${id}/like`);
}

export function likeComment(id) {
  return postApi(`comment/${id}/like`, undefined);
}

export function unlikeComment(id) {
  return postApi(`comment/${id}/unlike`, undefined);
}
